package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.RouterDao;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.pojo.Meta;
import com.pojo.Router;
import com.pojo.RouterJSON;
import com.service.RouterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RouterServiceImpl extends MPJBaseServiceImpl<RouterDao, Router> implements RouterService {
    @Resource
    private RouterDao routerDao;

    @Override
    public List<RouterJSON> getRouterJSONList(){
        List<RouterJSON> routerJSONList = new ArrayList<RouterJSON>();
        QueryWrapper<Router> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId",0);
        wrapper.eq("type",0);
        List<Router> routers = routerDao.selectList(wrapper);

        for (Router router : routers){
            RouterJSON routerJSON = new RouterJSON();
            QueryWrapper<Router> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parentId",router.getId());
            wrapper1.eq("type",1);
            List<Router> childrenRouters = routerDao.selectList(wrapper1);
            routerJSON.setId(router.getId());
            routerJSON.setPath(router.getPath());
            routerJSON.setName(router.getName());
            routerJSON.setComponent(router.getComponent());
            Meta meta = new Meta();
            meta.setTitle(router.getTitle());
            meta.setIcon(router.getIcon());
            routerJSON.setMeta(meta);
            List<RouterJSON> childrenRouterJSONList = new ArrayList<RouterJSON>();
            for (Router childrenRouter : childrenRouters){
                RouterJSON childrenRouterJSON = new RouterJSON();
                childrenRouterJSON.setId(childrenRouter.getId());
                childrenRouterJSON.setPath(childrenRouter.getPath());
                childrenRouterJSON.setName(childrenRouter.getName());
                childrenRouterJSON.setComponent(childrenRouter.getComponent());
                Meta childrenMeta = new Meta();
                childrenMeta.setTitle(childrenRouter.getTitle());
                childrenMeta.setIcon(childrenRouter.getIcon());
                childrenRouterJSON.setMeta(childrenMeta);
                childrenRouterJSONList.add(childrenRouterJSON);
            }
            routerJSON.setChildren(childrenRouterJSONList);
            routerJSONList.add(routerJSON);
        }

        return routerJSONList;
    }
}
